/**
 * @file lv_tutorial_hello_world
 *
 */

/*
 *-------------------------------------------------------------------------------
 * Create your first object: a "Hello world" label
 * ------------------------------------------------------------------------------
 *
 * If you have ported the LittlevGL you are ready to create content on your display.
 *
 * Now you will create a "Hello world!" label and align it to the middle.
 */

/*********************
 *      INCLUDES
 *********************/
#include "lv_example_button.h"
#if USE_LV_TUTORIALS

/*********************
 *      DEFINES
 *********************/

/**********************
 *      TYPEDEFS
 **********************/

/**********************
 *  STATIC PROTOTYPES
 **********************/

/**********************
 *  STATIC VARIABLES
 **********************/

/**********************
 *      MACROS
 **********************/

/**********************
 *   GLOBAL FUNCTIONS
 **********************/
lv_res_t bottom_btn_action(lv_obj_t * bottom_btn)
{
    int i, j;
    lv_obj_t * btn;
    lv_obj_t * label;
    lv_obj_t * child;
    char string[10];
    child = lv_obj_get_child(bottom_btn, NULL);
    if(NULL == child) {
        for(i = 0; i < 5; i++) {
            for(j = 0; j < 5; j++) {
                btn    = lv_btn_create(bottom_btn, NULL);
                lv_obj_set_size(btn, 64, 64);
                // lv_obj_set_pos(btn, j*64, i*64);
                lv_obj_align(btn, NULL, LV_ALIGN_IN_BOTTOM_RIGHT, -j*64, -i*64);
                label  = lv_label_create(btn, NULL);
                sprintf(string, "BT%d", i*5+j);
                lv_label_set_text(label, string);
                lv_btn_set_action(btn, LV_BTN_ACTION_CLICK, lv_obj_del);
            }
        }
    } else {
        printf("Nothing need to do!\r\n");
    }
    return LV_RES_OK;
}
/**
 * Create 5x5 button up a button, click the button(5x5) delete it, click the bottom button create 5x5 button up it
 */
void lv_example_button(void)
{
    int i, j;
    char string[10];
    lv_obj_t * bottom_btn = lv_btn_create(NULL, NULL);
    lv_btn_set_action(bottom_btn, LV_BTN_ACTION_CLICK, bottom_btn_action);
    lv_obj_t * btn;
    lv_obj_t * label;
    
    lv_obj_t * label_top;
    label_top = lv_layer_top();
    lv_obj_set_size(label_top, 800, 64);
    
    btn = lv_btn_create(lv_layer_top(), NULL);
    lv_obj_set_size(btn, 64, 64);
    lv_obj_align(btn, NULL, LV_ALIGN_CENTER, 0, 0);
    
    label_top = lv_label_create(btn, NULL);
    lv_label_set_text(label_top, "TBT");
    lv_obj_align(label_top, NULL, LV_ALIGN_CENTER, 0, 0);

    for(i = 0; i < 5; i++) {
        for(j = 0; j < 5; j++) {

            btn    = lv_btn_create(bottom_btn, NULL);
            lv_obj_set_size(btn, 64, 64);
            // lv_obj_set_pos(btn, j*64, i*64);
            lv_obj_align(btn, NULL, LV_ALIGN_IN_BOTTOM_RIGHT, -64*j, -64*i);
            label  = lv_label_create(btn, NULL);
            sprintf(string, "BT%d", i*5+j);
            lv_label_set_text(label, string);
            lv_btn_set_action(btn, LV_BTN_ACTION_CLICK, lv_obj_del);
        }
    }

    lv_scr_load(bottom_btn);
}

/**********************
 *   STATIC FUNCTIONS
 **********************/

#endif /*USE_LV_TUTORIALS*/
